//
//  NewFeatureCollectionVC.swift
//  HJWeibo
//
//  Created by huangjiong on 16/2/4.
//  Copyright © 2016年 huangjiong. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class NewFeatureCollectionVC: UICollectionViewController {

   //页面个数
    private let pageCount = 4
    //布局对象
    private var layout: HJCustomLayout = HJCustomLayout()
    
    init()
    {
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        
        //注册cell
        collectionView?.registerClass(HJCustomCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
    
    //MARK: - collectionView代理方法
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return pageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! HJCustomCollectionViewCell
        cell.imageIndex = indexPath.item
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        let idx = collectionView.indexPathsForVisibleItems().last!
        if idx.item == pageCount - 1
        {
            let cell = collectionView.cellForItemAtIndexPath(idx) as! HJCustomCollectionViewCell
            cell.startBtnAnimation()
        }
    }
    
    
}

//MARK: - 自定义cell
class HJCustomCollectionViewCell:UICollectionViewCell
{
    //图片
    private lazy var imgView = UIImageView()
    //图片下标
    private var imageIndex:Int?{
        didSet{
             imgView.image = UIImage(named: "new_feature_\(imageIndex! + 1)")
        }
    }
    //按钮
    private lazy var startButton:UIButton = {
       let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_button_highlighted"), forState: UIControlState.Highlighted)
        btn.hidden = true
        btn.addTarget(self, action: "startBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    /**
     点击开始按钮,去主页
     */
    func startBtnClick()
    {
        NSNotificationCenter.defaultCenter().postNotificationName(HJSwitchRootVcNoti, object: true)
    }
    
    //MARK: - 按钮动画
    func startBtnAnimation()
    {
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0.0, 0.0)
        startButton.userInteractionEnabled = false
        
        UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue: 0), animations: {
            () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
        }, completion: {
            (_) -> Void in
            self.startButton.userInteractionEnabled = true
        })
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI()
    {
        contentView.addSubview(imgView)
        imgView.frame = contentView.bounds
        contentView.addSubview(startButton)
        startButton.xmg_AlignInner(type: XMG_AlignType.BottomCenter, referView: contentView, size: nil, offset: CGPoint(x: 0, y: -160))
    }
}

//MARK: - 自定义布局对象
private class HJCustomLayout:UICollectionViewFlowLayout
{
    private override func prepareLayout() {
        
        //布局属性
        itemSize = UIScreen.mainScreen().bounds.size
        minimumInteritemSpacing = 0
        minimumLineSpacing = 0
        scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        //collectionView属性
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
        
    }
}
